package com.ewing.busi.api.pay.service;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.ewing.busi.order.constants.OrderStatus;
import com.ewing.busi.order.dao.OrderDetailDao;
import com.ewing.busi.order.dao.OrderInfoDao;
import com.ewing.busi.order.model.OrderDetail;
import com.ewing.busi.order.model.OrderInfo;
import com.ewing.busi.pay.constants.BusiType;
import com.ewing.busi.pay.constants.PayStatus;
import com.ewing.busi.pay.dao.PayHistoryDao;
import com.ewing.busi.pay.dao.PayWxPrepayidDetailDao;
import com.ewing.busi.pay.dao.PayWxResultDetailDao;
import com.ewing.busi.pay.model.PayHistory;
import com.ewing.busi.pay.model.PayWxPrepayidDetail;
import com.ewing.busi.pay.model.PayWxResultDetail;
import com.ewing.common.constants.IsEff;
import com.ewing.core.jdbc.BaseDao;
import com.ewing.core.wxpaysdk.api.paycallback.vo.PayNotifyCallBackReqParam;
import com.ewing.core.wxpaysdk.constants.ReturnCode;
import com.ewing.utils.JsonUtils;

@Repository("wxPayNotifyService")
public class WxPayNotifyService {
  /**
   * 默认生成的该类的LOG记录器，使用slf4j组件。避免产生编译警告，使用protected修饰符。
   */
  protected final static Logger LOG = LoggerFactory.getLogger(WxPayNotifyService.class);

  @Resource
  private PayWxPrepayidDetailDao payWxPrepayidDetailDao;
  @Resource
  private BaseDao baseDao;
  @Resource
  private OrderInfoDao orderInfoDao;
  @Resource
  private OrderDetailDao orderDetailDao;
  @Resource
  private PayHistoryDao payHistoryDao;
  @Resource
  private PayWxResultDetailDao payWxResultDetailDao;

  /**
   * 添加事务
   * 
   * @param reqParam created by Joeson on 2016年6月19日 下午5:48:52
   */
  public boolean payNotify(PayNotifyCallBackReqParam reqParam) {
    if (null == reqParam) {
      return false;
    }

    String bizId = reqParam.getAttach();
    PayWxPrepayidDetail prepayidDetail = payWxPrepayidDetailDao.getByLastestBizId(bizId);
    boolean isSuc = ObjectUtils.equals(reqParam.getResultCode(), ReturnCode.SUCCESS.name());

    prepayidDetail.setStatus(isSuc ? PayStatus.SUCCESS.getValue() : PayStatus.FAIL.getValue());
    baseDao.update(prepayidDetail);

    PayWxResultDetail resultDetail =
        payWxResultDetailDao.getByPrepayIdAndBizId(prepayidDetail.getPrePayId(), bizId);
    if (null == resultDetail) {
      resultDetail = new PayWxResultDetail();
    }
    resultDetail.setBizId(bizId);
    resultDetail.setPrePayId(prepayidDetail.getPrePayId());
    resultDetail.setReturnCode(reqParam.getResultCode());
    resultDetail.setReturnMsg(reqParam.getReturnMsg());
    resultDetail.setReturnDetail(JsonUtils.toJson(reqParam));
    resultDetail.setIseff(IsEff.EFFECTIVE.getValue());
    baseDao.saveOrUpdate(resultDetail);

    // 如果支付成功，需要同步order、orderdetail状态
    if (isSuc) {
      PayHistory payHistory = payHistoryDao.getByBizIdAndBusiType(bizId, BusiType.ORDER);
      payHistory.setStatus(PayStatus.SUCCESS.getValue());
      baseDao.update(payHistory);

      OrderInfo order = orderInfoDao.getByBizId(bizId);
      order.setStatus(OrderStatus.WAIT_SEND.getValue());
      baseDao.update(order);

      List<OrderDetail> detailList = orderDetailDao.getByBizId(bizId);
      for (OrderDetail detail : detailList) {
        detail.setStatus(OrderStatus.WAIT_SEND.getValue());
      }
      baseDao.updateMuti(detailList);
    }
    return true;
  }
}
